package com.mz.ozy.adapter;

import android.content.Context;
import android.view.View;
import android.view.ViewGroup;
import android.widget.LinearLayout;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.recyclerview.widget.RecyclerView;

import com.mz.ozy.R;
import com.mz.ozy.bean.ExchangeOrderBean;
import com.mz.ozy.bean.MyCardBean;

import java.util.List;

public class CardVoucherAdapter extends RecyclerViewAdapter<ExchangeOrderBean.CardMapBean, CardVoucherAdapter.ExChangeHolder> {

    private final String mGname;
    private String mPrice;
    private int mSelectPosition = 0;

    public CardVoucherAdapter(Context mContext, List<ExchangeOrderBean.CardMapBean> dataList, String price, String gname, int position) {
        super(mContext, dataList);
        this.mPrice = price;
        this.mGname = gname;
        mSelectPosition = position;
    }

    @NonNull
    @Override
    public CardVoucherAdapter.ExChangeHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        View itemView = mInflater.inflate(R.layout.item_card_voucher, parent, false);
        return new ExChangeHolder(itemView);
    }

    public void setSelectPosition(int selectPosition) {
        mSelectPosition = selectPosition;
        notifyDataSetChanged();
    }

    public int getSelectPosition() {
        return mSelectPosition;
    }

    @Override
    public void onBindHolder(CardVoucherAdapter.ExChangeHolder holder, ExchangeOrderBean.CardMapBean dataItem, int position) {
        holder.tvMoney.setText(mPrice);
        holder.tvName.setText(dataItem.getCardname());
        holder.tvType.setText("规格: " + mGname);
        holder.tvData.setText("有效时间: " + dataItem.getStarttime() + "-" + dataItem.getEndtime());

        if (mSelectPosition == position) {
            holder.linearLayout.setBackgroundResource(R.mipmap.bg_card_voucher_press);
        } else {
            holder.linearLayout.setBackgroundResource(R.mipmap.bg_card_voucher_normal);
        }
    }

    @Override
    public long getItemId(int position) {
        return position;
    }


    static class ExChangeHolder extends RecyclerView.ViewHolder {

        TextView tvName;
        TextView tvType;
        TextView tvMoney;
        TextView tvData;
        LinearLayout linearLayout;

        public ExChangeHolder(View itemView) {

            super(itemView);
            linearLayout = itemView.findViewById(R.id.ll);
            tvMoney = itemView.findViewById(R.id.tv_money);
            tvData = itemView.findViewById(R.id.tv_data);

            tvName = itemView.findViewById(R.id.tv_name);
            tvType = itemView.findViewById(R.id.tv_type);
        }
    }
}
